/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 *
 * @author 10515
 */
public class UserService {
    private static final String FILE_PATH = "./data/Users.txt";
    private static String currentUser;
    private static  File file ;
    
   

    public static String getCurrentUser() {
        return currentUser;
    }
    
    public static boolean login(String userName, String password){
        
        try (FileInputStream fis = new FileInputStream(FILE_PATH);
             InputStreamReader isr = new InputStreamReader(fis);
             BufferedReader br = new BufferedReader(isr)) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] strs = line.split("\\s+");
                if (strs[0].equals(userName) && strs[1].equals(password)) {
                    currentUser = userName;
                    file = new File("./data/"+UserService.getCurrentUser()+".txt");
                    if (!file.exists()) {
                        try {
                            file.createNewFile();
                        } catch (IOException e) {
                            // TODO: handle exception
                            e.printStackTrace();
                        }
                    }
                    return true;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
    
    public static boolean register(String userName, String password, String confirmPassword) {

        

        try (FileInputStream fis = new FileInputStream(FILE_PATH);
             InputStreamReader isr = new InputStreamReader(fis);
             BufferedReader br = new BufferedReader(isr)) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] strs = line.split("\\s+");
                if (strs[0].equals(userName)) {
                    return false;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try(FileOutputStream fos = new FileOutputStream(FILE_PATH, true);
            OutputStreamWriter osw = new OutputStreamWriter(fos);
            BufferedWriter bw = new BufferedWriter(osw)) {
            bw.write(userName +" "+ password + System.lineSeparator());
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}
